package com.ly.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.reggie.common.R;
import com.ly.reggie.dto.DishDto;
import com.ly.reggie.dto.SetmealDto;
import com.ly.reggie.mapper.SetmealDishMapper;
import com.ly.reggie.pojo.*;
import com.ly.reggie.service.CategoryService;
import com.ly.reggie.service.SetMealService;
import com.ly.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetMealController {

    @Autowired
    SetMealService setMealService;

    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    CategoryService categoryService;

    /**
     * 套餐管理分页查询
     */
    @GetMapping("/page")
    public R<Page> pageShow(int page, int pageSize, String name) {
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);

        //查询条件构造器
        LambdaQueryWrapper<Setmeal> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加分页条件
        dishLambdaQueryWrapper.like(name != null, Setmeal::getName, name);
        //添加排序条件
        dishLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setMealService.page(pageInfo, dishLambdaQueryWrapper);

        //对象拷贝,将pageInfo拷贝到pageDto中，但是排除records属性
        BeanUtils.copyProperties(pageInfo, setmealDtoPage, "records");

        //获取Setmeal的records属性
        List<Setmeal> records = pageInfo.getRecords();
        //使用lambada表达式转换SetmealDto中的records属性
        List<SetmealDto> SetmealDtoRecords = (records).stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //将dish中的属性复制到setmealDto中
            BeanUtils.copyProperties(item, setmealDto);
            //获取Setmeal类中的categoryId属性
            Long categoryId = item.getCategoryId();
            //根据categoryId属性使用categoryService类查找菜品的category
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //根据category类中的getName方法找到categoryName
                String categoryName = category.getName();
                //将categoryName赋给dishDto中的CategoryName属性
                setmealDto.setCategoryName(categoryName);
            }
            //返回新构造的dishDto
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(SetmealDtoRecords);

        return R.success(setmealDtoPage);
    }


    /**
     * 新增套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info(setmealDto.toString());

        setMealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /**
     * 修改为停售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> changeStatus0(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            setmeal.setStatus(0);

            setMealService.updateById(setmeal);
        }
        return R.success("修改状态成功");
    }

    /**
     * 修改为起售
     *
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> changeStatus1(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            setmeal.setStatus(1);

            setMealService.updateById(setmeal);
        }
        return R.success("修改状态成功");
    }


    /**
     * 展示修改页面
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> changeSetmeal(@PathVariable Long id) {
        log.info("id = {}", id);
        SetmealDto setmealDto = setMealService.showById(id);

        return R.success(setmealDto);
    }

    /**
     * 保存修改套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto) {
        setMealService.updateWithDish(setmealDto);

        return R.success("修改成功");
    }


    /**
     * 删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteSetmeal(Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = setMealService.getById(id);
            Integer status = setmeal.getStatus();
            if (status == 0) {
                setMealService.removeById(id);
            } else {
                return R.error("套餐正在售卖无法删除");
            }
        }

        return R.success("删除成功");
    }


    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<SetmealDto>> addSetmeal(Setmeal setmeal) {
        //获取套餐种类
        Long categoryId = setmeal.getCategoryId();
        //构造查询构造器
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, categoryId);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus, 1);
        //添加排序条件
        setmealLambdaQueryWrapper.orderByDesc(Setmeal::getUpdateTime);
        //获取到Setmeal集合
        List<Setmeal> setmealList = setMealService.list(setmealLambdaQueryWrapper);

        //新建一个SetmealDto集合
        List<SetmealDto> setmealDtoList = setmealList.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //将dish中的属性复制到setmealDto中
            BeanUtils.copyProperties(item, setmealDto);
            //获取Setmeal类中的categoryId属性
            Long categoryId_Setmeal = item.getCategoryId();
            //根据categoryId属性使用categoryService类查找菜品的category
            Category category_Setmeal = categoryService.getById(categoryId_Setmeal);
            if (category_Setmeal != null) {
                //根据category类中的getName方法找到categoryName
                String categoryName = category_Setmeal.getName();
                //将categoryName赋给dishDto中的CategoryName属性
                setmealDto.setCategoryName(categoryName);
            }

            //添加菜品信息,用于查询套餐对应的菜品
            Long id = item.getId();
            //构造体条件查询器
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            //添加查询条件
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getDishId,id);
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
            //添加查询出的菜品
            setmealDto.setSetmealDishes(setmealDishList);

            return setmealDto;

        }).collect(Collectors.toList());

        return R.success(setmealDtoList);
    }

}
